//
//  movieDetailViewController.m
//  movie
//
//  Created by 白浩洋 on 2018/4/7.
//  Copyright © 2018年 白浩洋. All rights reserved.
//

#import "movieDetailViewController.h"
#import "HotSearchCollectionViewCell.h"
#import "HotHeadFreeCollectionReusableView.h"
#import "MoviePlayViewController.h"
#import "collectionHeaderView.h"
#import "movieDetailViewController.h"
@interface movieDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)NSMutableArray *dataSource;
@property (nonatomic,strong)UITableView *tabView;
@end

@implementation movieDetailViewController
{
    NSInteger index;
    BOOL isDown;
    NSMutableArray *DateArr;
    CGFloat cellWidth;
    CGFloat cellHeight;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    cellWidth = ScreenWidth - PXChange(100);
    cellHeight = (ScreenWidth- PXChange(100)) * 3/4.0f;
    NSLog(@"%@",self.typeDic);
    [self addCustomTitleWithTitle:self.typeDic[@"des"]];
    [SVProgressHUD showInfoWithStatus:@"正在加载中。。。"];
    [self.tabView.mj_header beginRefreshing];
}
- (void)requestDate{
    BmobQuery *bquery = [BmobQuery queryWithClassName:@"video"];
    bquery.limit = 12;
    bquery.skip = index * 12;
    NSString *type =self.typeDic[@"type"];
    [bquery whereKey:@"type" equalTo:type];
    [bquery orderByDescending:@"yuedunum"];
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        [SVProgressHUD dismiss];
        if(error){
            [self.tabView.mj_header endRefreshing];
            [self.tabView.mj_footer endRefreshing];
        }else{
            if(array.count > 0 && isDown){
                [self.dataSource removeAllObjects];
            }
            for (BmobObject *obj in array) {
                NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
                [dic setObject:obj.objectId forKey:@"id"];
                [dic setObject:[obj objectForKey:@"title"] forKey:@"des"];
                [dic setObject:[obj objectForKey:@"covimage"] forKey:@"imgurl"];
                [dic setObject:[obj objectForKey:@"type"] forKey:@"type"];
                [dic setObject:[obj objectForKey:@"videoUrl"] forKey:@"videoUrl"];
                [dic setObject:[obj objectForKey:@"shoujia"] forKey:@"shoujia"];
                [dic setObject:[obj objectForKey:@"yuedu"] forKey:@"yuedu"];
                if([obj objectForKey:@"shichang"]){
                [dic setObject:[obj objectForKey:@"shichang"] forKey:@"shichang"];
                }
                [self.dataSource addObject:dic];
            }
            [self.tabView.mj_header endRefreshing];
            [self.tabView.mj_footer endRefreshing];
            [self.tabView reloadData];
        }
    }];
}
-(NSMutableArray *)dataSource{
    if(!_dataSource){
        _dataSource = [[NSMutableArray alloc]init];
    }
    return _dataSource;
}
- (UITableView *)tabView{
    if(!_tabView){
        _tabView = [[UITableView alloc]initWithFrame:CGRectMake(0,PXChange(20), ScreenWidth, self.view.height - PXChange(40) -iPhoneXTabHeigth)];
        _tabView.delegate = self;
        _tabView.dataSource =self;
        _tabView.separatorStyle = UITableViewCellSeparatorStyleNone;
//        _tabView.backgroundColor = [UIColor colorWithHexString:BH_Color_Main];
        _tabView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            isDown = YES;
            index = 0;
            [self requestDate];
        }];
        _tabView.mj_footer =  [MJRefreshAutoFooter footerWithRefreshingBlock:^{
            index ++;
            isDown = NO;
            [self requestDate];
        }];
        if (@available(iOS 11.0, *)) {
            _tabView.estimatedRowHeight = 0;
            _tabView.estimatedSectionFooterHeight = 0;
            _tabView.estimatedSectionHeaderHeight = 0;
            _tabView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        [self.view addSubview:_tabView];
    }
    return _tabView;
}
#pragma mark - tableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return cellHeight + PXChange(100);
}
#pragma mark - tableViewDelegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if(!cell){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    [cell.contentView removeAllSubviews];
    NSString *imgUrl =  self.dataSource[indexPath.row][@"imgurl"];
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, cellWidth, cellHeight)];
    [image sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@"jpghold"]];
    image.center = CGPointMake(ScreenWidth/2.0f, image.height/2.0f);
    image.layer.cornerRadius  =PXChange(20);
    image.clipsToBounds = YES;
    [cell.contentView addSubview:image];
    UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth - PXChange(120) , 0)];
    lab.text = self.dataSource[indexPath.row][@"des"];;
    lab.textColor = [UIColor colorWithHexString:@"#333333"];
    lab.font = [UIFont systemFontOfSize:PXChange(28)];
    lab.numberOfLines = 0;
    [lab sizeToFit];
    lab.center = CGPointMake(ScreenWidth/2.0f, image.bottom +lab.height/2.0f +PXChange(10));
    [cell.contentView addSubview:lab];

//    UILabel *yuedulab = [[UILabel alloc]initWithFrame:CGRectZero];
//    yuedulab.text = [NSString stringWithFormat:@"%@次数观看",self.dataSource[indexPath.row][@"yuedu"]];
//    yuedulab.textColor = [UIColor colorWithHexString:@"#666666"];
//    yuedulab.font = [UIFont systemFontOfSize:PXChange(15)];
//    yuedulab.numberOfLines = 0;
//    [yuedulab sizeToFit];
//    yuedulab.center = CGPointMake(image.right + PXChange(20) + yuedulab.width/2.0f, PXChange(200) - yuedulab.height/2.0f - PXChange(20));
//    [cell.contentView addSubview:yuedulab];
//    UILabel *shoujialab = [[UILabel alloc]initWithFrame:CGRectZero];
//    shoujialab.text =[NSString stringWithFormat:@"收取:%@星",self.dataSource[indexPath.row][@"shoujia"]];
//    shoujialab.textColor = [UIColor colorWithHexString:@"#666666"];
//    shoujialab.font = [UIFont systemFontOfSize:PXChange(15)];
//    shoujialab.numberOfLines = 0;
//    [shoujialab sizeToFit];
//    shoujialab.center = CGPointMake(yuedulab.right + PXChange(40) + shoujialab.width/2.0f, yuedulab.centerY);
//    [cell.contentView addSubview:shoujialab];
//    UILabel *shichang = [[UILabel alloc]initWithFrame:CGRectZero];
//    if(self.dataSource[indexPath.row][@"shichang"]){
//        shichang.text = [NSString stringWithFormat:@"时长:%@分",self.dataSource[indexPath.row][@"shichang"]];
//    }
//    shichang.textColor = [UIColor colorWithHexString:@"#333333"];
//    shichang.font = [UIFont systemFontOfSize:PXChange(15)];
//    shichang.numberOfLines = 0;
//    [shichang sizeToFit];
//    shichang.center = CGPointMake(shoujialab.right + PXChange(40) + shichang.width/2.0f, yuedulab.centerY);
//    [cell.contentView addSubview:shichang];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *arr = [NBATools gethistory];
    NSInteger i = [self.dataSource[indexPath.row][@"shoujia"] intValue];
    NSInteger j = [self.dataSource[indexPath.row][@"yuedunum"] intValue];
    j+=3;
    [NBATools setyuedu:[NSNumber numberWithInteger:j] WithID:self.dataSource[indexPath.row][@"id"] withTabName:@"video"];
    for (int i = 0; i<arr.count; i++) {
        NSDictionary *dic = arr[i];
        if([dic[@"videoUrl"] isEqualToString:self.dataSource[indexPath.row][@"videoUrl"]]){
            MoviePlayViewController *mv = [[MoviePlayViewController alloc]init];
            mv.hidesBottomBarWhenPushed = YES;
            mv.MovieName = self.dataSource[indexPath.row][@"des"];
            mv.MovieUrl =self.dataSource[indexPath.row][@"videoUrl"];
            mv.shoucangdic = self.dataSource[indexPath.row];
            [self.navigationController pushViewController:mv animated:YES];
            return;
        }
    }
    
    NSInteger totalStar = [NBATools getstar];
    if(totalStar - i > 0){
        [NBATools setstar:totalStar - i];
        [NBATools setstarOnline:(totalStar - i)];
        NSArray * hisArr = [NBATools gethistory];
        NSMutableArray *mutArr = [[NSMutableArray alloc]initWithArray:hisArr];
        [mutArr insertObject:self.dataSource[indexPath.row] atIndex:0];
        [NBATools sethistory:mutArr];
        MoviePlayViewController *mv = [[MoviePlayViewController alloc]init];
        mv.hidesBottomBarWhenPushed = YES;
        mv.MovieName = self.dataSource[indexPath.row][@"des"];
        mv.MovieUrl =self.dataSource[indexPath.row][@"videoUrl"];
        mv.shoucangdic = self.dataSource[indexPath.row];
        [self.navigationController pushViewController:mv animated:YES];
    }else{
        UIAlertController *alert = [NBATools showAlert];
        [self presentViewController:alert animated:YES completion:nil];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
